package com.liu.lab04.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebInitParam;
import java.io.IOException;

@WebFilter(filterName = "CharacterEncodingFilter", urlPatterns = {"/*"},
		initParams = {
		@WebInitParam(name = "encoding", value = "utf-8")
})
public class CharacterEncodingFilter implements Filter {
	private static String encoding;
	public void destroy() {
		System.out.println("CharacterEncodingFilter------destroy");
	}
	public void doFilter(ServletRequest request, ServletResponse response,
						 FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(encoding);
		response.setContentType("text/html;charset="+encoding);
		chain.doFilter(request, response);
	}
	public void init(FilterConfig fConfig) throws ServletException {
		encoding = fConfig.getInitParameter("encoding");
		System.out.println(encoding);
	}
}
